package saucaca.fragment;

import java.util.ArrayList;
import java.util.List;

import lemon.kenhnhacvn.BaseEngine;
import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;

import org.json.JSONException;

import saucaca.Adapter.ArtistAdapter;
import saucaca.CustomView.XListView;
import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.SingerObject;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;


public class ArtistList extends Fragment implements saucaca.CustomView.XListView.IXListViewListener{
	public static final String TAG = ArtistList.class.getSimpleName();
	List<SingerObject> listSinger;
	String sType = "2";
	String areaCode = "";
	String page = "0";
	String pageSize = "";
	int xpage = 0;

	MainActivity mActivity;
	private XListView listView;
	boolean loadingMore;
	private View mView_EmptyLoading = null;
	private ProgressBar progressBar;
	ImageView imgEmpty, imgError;
	TextView txtStatus;
	boolean isFromMenu;
	TextView txtTitle;
	ArtistAdapter adapter;
	Context context;
	private SwipeRefreshLayout mSwipeLayout;
	public ArtistList(Context context, String areaCode) {
		this.context = context;
		this.areaCode = areaCode;
		new GetFromServices().execute("");
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mActivity = (MainActivity) getActivity();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View android = inflater.inflate(R.layout.listview, container, false);
		findviewByID(android);
		listSinger = new ArrayList<SingerObject>();
		adapter = new ArtistAdapter(context, listSinger);
		listView.setAdapter(adapter);
		return android;
	}

	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private void findviewByID(View view) {
		listView = (XListView) view.findViewById(R.id.layoutlistview_listview);
		mView_EmptyLoading = view.findViewById(R.id.empty_loading);
		listView.setEmptyView(mView_EmptyLoading);
		listView.setPullLoadEnable(true);
		listView.setXListViewListener(this);
		listView.startProgressBar();
		progressBar = (ProgressBar) mView_EmptyLoading.findViewById(R.id.empty_loading_progress);
		imgEmpty = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_NoItem);
		imgError = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_Error);
		txtStatus = (TextView) mView_EmptyLoading.findViewById(R.id.empty_loading_text);
		mSwipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
		mSwipeLayout.setOnRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				new GetFromServices().execute("");
//				loadingMore = false;
//				listView.stopProgressBar();
//				stop();
			}
		});
		mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
				android.R.color.holo_green_light, android.R.color.holo_orange_light,
				android.R.color.holo_red_light);
	}

	class GetFromServices extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... param) {
			page = Integer.toString(xpage);
			loadingMore = true;
			String str = WServiceLib.artist(page, pageSize, areaCode);
			BaseEngine.showLogformLongString(str);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result != null)
				try {
					if (listSinger != null) {
						List<SingerObject> singerObjects = JsonReader.getArtistReader(result);
						if (singerObjects != null && singerObjects.size() > 0) {
							for (int i = 0; i < singerObjects.size(); i++) {
								boolean isAdded=false;
								for (int j = 0; j < listSinger.size(); j++) {
									if (listSinger.get(j).getSINGER_ID().equals(singerObjects.get(i).getSINGER_ID())) {
										isAdded=true;
									}
								}
								if(!isAdded)
									listSinger.add(singerObjects.get(i));
							}
						}
						adapter.notifyDataSetChanged();
						loadingMore = false;
						listView.stopProgressBar();
						if(mSwipeLayout.isRefreshing())
							mSwipeLayout.setRefreshing(false);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
		}
	}

	public void stop() {
		listView.stopRefresh();
		listView.stopLoadMore();
	}

	@Override
	public void onRefresh() {
		if (!loadingMore) {
			xpage += 1;
			new GetFromServices().execute("");
		} else
			stop();
	}

	@Override
	public void onLoadMore() {
		if (!loadingMore) {
			xpage += 1;
			new GetFromServices().execute("");
		} else
			stop();
	}
}
